import { Component, Label, _decorator, director } from "cc"
import JSWebView from "../JSWebView"
import APIHelper from "../api/APIHelper"
import Orientation from "../components/utils/Orientation"
import StorageHelper from "../helper/StorageHelper"
import PopupHelper from "../helper/PopupHelper"
import ToastDialog from "../components/popup/ToastDialog"
import PrefabHelper from "../helper/PrefabHelper"
import { prefabList } from "../Const"
const { ccclass,property } = _decorator
@ccclass
export default class Loading extends Orientation{
    @property(Label)
    private loadingLabel:Label=null
    protected onLoad(): void {
        console.log("Loading loaded")
        //本地存储
        const storage=StorageHelper.getInstance()
        storage.init()
        //预加载prefab
        PrefabHelper.getInstance().preload(prefabList)
        //
        const api=APIHelper.getInstance()
        director.preloadScene('main',(completedCount : number, totalCount : number)=>{
            //this.progress_bar.progress=completedCount/totalCount
            this.loadingLabel.string='加载'+(completedCount/totalCount*100).toFixed(2)+'%……'
            if(completedCount==totalCount){
                this.loadingLabel.string='加载完成'
            }

        },()=>{
            const urlParams = new URLSearchParams(window.location.search)
            const token=urlParams.get('token')
            if(token&&window.location.hostname=='ieh52.fenzhidao.com'){
                api.init({token:token,url:'https://app2.fenzhidao.com/'})
                director.loadScene('main')
            }else if(token&&window.location.hostname=='ieh5.fenzhidao.com'){
                api.init({token:token,url:'https://dc.fenzhidao.com/'})//https://dc.fenzhidao.com/inEnglish
                director.loadScene('main')
            }else{
                const config=storage.api_config
                const url=config.debug_links[config.index]
                api.init({token:'',url:url})
                api.logon({account:config.account||'13423676525',code:'888888'}).then((res:any)=>{
                    console.log(res)
                    //api.setToken(res.token)
                    api.init({token:res.token,url:url})
                    director.loadScene('main')
                }).catch(err=>{
                    console.log(err)
                    /*const pop=PopupHelper.getInstance()
                    pop.openDialog('ToastDialog').then(node=>{
                        node.getComponent(ToastDialog).dataSource={
                            title:'提示',
                            content:'登录失败!',
                            type:'warn',
                        }
                    })*/
                    alert('登录失败!')
                })
            }
        })
        //隐藏启动页
        const el=document.getElementById('start-page')
        if(el)
        el.style.display='none'
    }
}